import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ListProvider1 with ChangeNotifier {
  List<ItemData> _list;

  get list => _list;

  ListProvider1() {
    _list = List();
    for (int i = 0; i < 20; i++) {
      _list.add(ItemData(i.toString(), i.toString()));
    }
    notifyListeners();
  }

  changeContent(int index) {
    _list[index].content = "aaa";
    _list[index].title = "bbb";
    notifyListeners();
  }
}

class TestProviderList1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => ListProvider1()),
      ],
      child: Scaffold(
        appBar: AppBar(
          title: Text("TestProviderList"),
        ),
        body: Content(),
      ),
    );
  }
}

class Content extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<ItemData> list = context.watch<ListProvider1>().list;
    return ListView.builder(
      itemBuilder: (ctx, index) {
        return Item(
          itemData: list[index],
          index: index,
        );
      },
      itemExtent: 100,
      itemCount: list?.length ?? 0,
    );
  }
}

class Item extends StatelessWidget {
  final ItemData itemData;
  final int index;

  const Item({Key key, this.itemData, this.index}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        context.read<ListProvider1>().changeContent(index);
      },
      child: Container(
        height: 100,
        child: Center(
          child: Column(
            children: [
              Text(itemData.title),
              Text(itemData.content),
            ],
          ),
        ),
      ),
    );
  }
}

class ItemData {
  String title;
  String content;

  ItemData(this.title, this.content);

  @override
  String toString() {
    return 'ItemData{title: $title, content: $content}';
  }
}
